package com.lmonkey.servlet.cart;

import com.lmonkey.dao.Basedao;
import com.lmonkey.servic.CARTDao;
import com.lmonkey.servic.PRODUCTDao;
import com.lmonkey.shop.CART;
import com.lmonkey.shop.PRODUCT;
import com.lmonkey.shop.USER;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/shangc/cartadd")
public class CartAdd extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取参数
        String count = request.getParameter("count");
        String pid = request.getParameter("id");
        String url = request.getParameter("url");

        PRODUCT p = null;
        //获取session对象
        HttpSession session = request.getSession();
        String isLogin = (String)session.getAttribute("isLogin");
        USER user = (USER)session.getAttribute("name");
        //判断用户是否登录
        if (user != null && isLogin.equals("1")){
            String uid = (String)user.getuser_id();

            //通过用户id和商品id，查找是否有记录

            CART srcsp = CARTDao.getcartshop(uid, Integer.parseInt(pid));
            if (srcsp != null){
                    int srccount = srcsp.getCart_quantity();
                    int newscount = srccount + Integer.parseInt(count);

                    if (newscount >= 5){
                        newscount = 5;
                    }
                    CARTDao.updatenum(srcsp.getCart_id(),newscount);
                }else {
                    if (pid != null){
                        p = PRODUCTDao.selectByid(Integer.parseInt(pid));
                        //创建购物车实体
                        CART cart = new CART(
                                0,
                                p.getProduct_filename(),
                                p.getProduct_name(),
                                p.getProduct_price(),
                                Integer.parseInt(count),
                                p.getProduct_stock(),
                                p.getProduct_id(),
                                uid,
                                1
                        );
                        //连接数据库
                        Connection conn = Basedao.getconn();
                        PreparedStatement ps = null;
                        try {
                            String sql = "insert into lmonkey_cart values(null,?,?,?,?,?,?,?,?)";
                            ps = conn.prepareStatement(sql);
                            ps.setObject(1,cart.getCart_p_filename());
                            ps.setObject(2,cart.getCart_p_name());
                            ps.setFloat(3,cart.getCart_p_price());
                            ps.setInt(4,cart.getCart_quantity());
                            ps.setInt(5,cart.getCart_p_stock());
                            ps.setInt(6,cart.getCart_p_id());
                            ps.setObject(7,cart.getCart_u_id());
                            ps.setInt(8,cart.getCart_valid());
                            ps.executeUpdate();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
            }
        }else {
            PrintWriter out = response.getWriter();
            out.println("<script>alert('登录后，再购买');location.href='login.jsp';</script>");
            out.close();
            return;
        }

        if (url.equals("z")){
            response.sendRedirect("showcart");
        }else{
            response.sendRedirect("selectproductview?id="+pid);
        }
    }
}
